import {BaseService} from '../shared/service/base.service';
import {Injectable} from '@angular/core';
import {ApiUrl} from '../config/api-url';

@Injectable()
export class UserService extends BaseService {
  //查询所有 带分页
  findUserList(pageHelper, callback?) {
    this.findListByKeyword(ApiUrl.UserUrl + '/managers', pageHelper).subscribe(
      data => {
        let res = this.pageHandle(data, pageHelper);
        callback && callback(res);
      });
  }

  /**查询用户并且查询角色  保留后期可以自己增加搜索按钮*/
  // findUserAndFindUserRole(pageHelper,body,callback?) {
  //   console.log(pageHelper)
  //   this.findListByMuchCondition2(ApiUrl.UserUrl + '/findUserAndFindUserRole', body).subscribe(
  //     data => {
  //       let res = this.pageHandle(data,pageHelper);
  //       callback && callback(res);
  //     });
  // }
  //更新用户状态
  updateUserStatusById(user, callback?) {
    this.put(ApiUrl.UserUrl + '/updateUserStatusById', user).subscribe(data => {
      this.getResponse(data);
      callback && callback(data);

    });
  }

  addUser(data, callback?) {
    this.post(ApiUrl.UserUrl + '/userAdd', data).subscribe(data => {
      callback && callback(data);
    });
  }

  resetPwdById(data) {
    this.put(ApiUrl.UserUrl + '/resetPwdById', data).subscribe(data => {
      this.getResponse(data);
    });
  }

  addUserAndAddRole(data, callback?) {
    this.post(ApiUrl.UserUrl + '/addUserAndAddRole', data).subscribe(data => {
      callback && callback(data);
    });
  }

  resetOpwdById(data) {
    this.put(ApiUrl.UserUrl + '/resetPwdById', data).subscribe(data => {
      this.getResponse(data, '修改成功新操作密码为手机号');
    });
  }

  modifyOPwdById(data, callback?) {
    this.put(ApiUrl.UserUrl + '/modifyOPwdById', data).subscribe(data => {
      callback && callback(data);
    });
  }

  update(data, callback?) {
    this.put(ApiUrl.UserUrl + '/updateInfo', data).subscribe(data => {
      callback && callback(data);
    });
  }

  checkMobile(data, callback?) {
    this.get(ApiUrl.UserUrl + '/check?mobile=' + data).subscribe(data => {
      callback && callback(data);
    });
  }

  checkUsername(data, callback?) {
    this.get(ApiUrl.UserUrl + '/check?username=' + data).subscribe(data => {
      callback && callback(data);
    });
  }

  checkIsNotMobile(data, callback?) {
    this.post(ApiUrl.UserUrl + '/checkThis', data).subscribe(data => {
      callback && callback(data);
    });
  }

  checkIsNotUsername(data, callback?) {
    this.post(ApiUrl.UserUrl + '/checkThis', data).subscribe(data => {
      callback && callback(data);
    });
  }

  deleteUser(data, callback?) {
    this.delete(ApiUrl.UserUrl + '/delete', data).subscribe(data => {
      callback && callback(data);
    });
  }

  /**查询用户并且查询角色  保留后期可以自己增加搜索按钮*/
  findStaffListByAgencyId(pageHelper, body, callback?) {
    console.log(pageHelper);
    this.findListByMuchCondition(ApiUrl.UserUrl + '/findStaffListByAgencyId', body).subscribe(
      data => {
        let res = this.pageHandle(data, pageHelper);
        callback && callback(res);
      });
  }

  findUserOperate(userId, callback?) {
    this.get(ApiUrl.UserUrl + '/findUserOperate?id=' + userId).subscribe(data => {
      callback && callback(data);
    });

  }

  // 修改系统管理用户管理的用户密码
  updatePwd(body, callback?) {
    this.put(ApiUrl.UserUrl + '/updatePwd', body).subscribe(data => {
      this.getResponse(data, '密码修改成功', data.msg);
      callback && callback(data);
    });
  }

  findUsername(body, callback?) {
    this.get(ApiUrl.UserUrl + '/findUsername?agencyType=' + body.agencyType + '&title=' + body.title).subscribe(data => {
      callback && callback(data);
    });
  }

  getAccountInfo(callback?) {
    this.get(ApiUrl.UserUrl + '/getAccountInfo').subscribe(data => {
      callback && callback(data);
    });
  }

  getUserAgencyList(id, callback?) {
    this.get(ApiUrl.UserUrl + '/getUserAgencyList?id=' + id).subscribe(data => {
      callback && callback(data);
    });
  }

  /**
   * 修改用户管理信息   暂时留着
   * @param user
   * @param param2
   */
  updateUserAndAddRole(user, callback?) {
    this.put(ApiUrl.UserUrl + '/updateUserAndAddRole', user).subscribe(data => {
      callback && callback(data);
    });
  }

  getStoreIdList(id, callback?) {
    this.get(ApiUrl.UserUrl + '/getStoreIdList?id=' + id).subscribe(data => {
      callback && callback(data);
    });
  }

  checkExistPassword(userId, callback?) {
    this.get(ApiUrl.UserUrl + '/checkExistPassword?userId=' + userId).subscribe(data => {
      callback && callback(data);
    });
  }

  /**
   * 根据关键字查询员工列表,带分页
   */
  findByNameAndMobile(pageHelper, body, callback?) {
    console.log(pageHelper);
    this.findListByMuchCondition(ApiUrl.UserUrl + '/findByNameAndMobile', body).subscribe(
      data => {
        let res = this.pageHandle(data, pageHelper);
        callback && callback(res);
      });
  }

  /**
   * 查询离职员工,带分页
   * @param pageHelper
   * @param body
   * @param callback
   */
  findDimissionUser(pageHelper, body, callback?) {
    console.log(pageHelper);
    this.findListByMuchCondition(ApiUrl.UserUrl + '/findDimissionUser', body).subscribe(
      data => {
        let res = this.pageHandle(data, pageHelper);
        callback && callback(res);
      });
  }

  /**
   * 获取登录用户信息
   */
  userInfo(callback?){
    this.get(ApiUrl.UserUrl + '/userInfo').subscribe(data => {
      callback && callback(data);
    });
  }

  /**
   * 根据主键用户UserID 删除用户和用户角色表数据
   *
   * @param callback
   */
  deleteUserAndUserRole(userId,roleId,callback?){
    this.get(ApiUrl.UserUrl + '/deleteUserAndUserRole?userId='+userId+'&roleId='+roleId).subscribe(data =>{
      callback && callback(data);
    })
  }
}
